ge_dir = '/autofs/space/clive_001/users/adriana/GE/MNE';
svm_dir = '/autofs/space/clive_001/users/adriana/GE_SVM/';
SubjectNames = {'GE_05', 'GE_06', 'GE_07', 'GE_08', 'GE_09', 'GE_10', 'GE_11', 'GE_12', 'GE_13', 'GE_14', 'GE_15','GE_16'}; %  
roisets = {'newSMG', 'newpMTG', 'TempPole', 'FrontalPole', 'CentGyri'};

for i_subject = 1:length(SubjectNames)
    subject = SubjectNames{i_subject};
    
    for i_roi = 1:length(roisets);
        roi = roisets{i_roi};
        
        nvs_filename = sprintf('%s/%s/epochs/%s_%s_nvs_all.mat', svm_dir, subject, subject, roi);
        nvs = load(nvs_filename);
        lvn_filename = sprintf('%s/%s/epochs/%s_%s_lvn_all.mat', svm_dir, subject, subject, roi);
        lvn = load(lvn_filename, 'condition_index');
        pos_filename = sprintf('%s/%s/epochs/%s_%s_pos_all.mat', svm_dir, subject, subject, roi);
        pos = load(pos_filename, 'condition_index');
%        half_filename = sprintf('%s/%s/epochs/%s_%s_half_all.mat', svm_dir, subject, subject, roi);
%        half = load(half_filename, 'condition_index');
%        third_filename = sprintf('%s/%s/epochs/%s_%s_third_all.mat', svm_dir, subject, subject, roi);
%        third = load(third_filename, 'condition_index');
        
        all_events = {};
		n_nvsevents = 0;
        
        for block=1:16
            logfile = sprintf('%s/%s/logs/%s_B%d_ave.log', ge_dir, subject, subject, block);
			nvseventfile = sprintf('%s/%s/events/%s_B%d_grouped_nvs.eve', svm_dir, subject, subject, block);
			nvsevents = load(nvseventfile);
            fid = fopen(logfile);
            logtext = textscan(fid, '%s %s %s %s %s', 'Delimiter', '\t');
            logtext_all = logtext{1}(contains(logtext{1}, '[all]'));
            logtext_all_split = split(logtext_all, {'      '});
            logtext_all_split = strtrim(logtext_all_split);
            [~, block_events] = strtok(logtext_all_split(:,2));
            block_events = strtok(block_events);
			if length(block_events) ~= length(nvsevents) && i_roi==1
            	warning('Block number %d has %d log events and %d nvs events (%s)', block, length(block_events), length(nvsevents), subject);
			end
			n_nvsevents = n_nvsevents+length(nvsevents);
            all_events = [all_events; block_events];
        end
        
		if length(all_events) ~= length(lvn.condition_index) | length(all_events) ~= length(pos.condition_index) 
			if i_roi == 1
				warning('Subject %s has a mismatch in the number of events.', subject);
				fprintf('Log events: %d\tNVS events: %d (%d)\tLVN events: %d\tPOS events: %d\n', length(all_events),...
					 n_nvsevents, size(nvs.epoch_data, 3), length(lvn.condition_index), length(pos.condition_index));
			end
			continue
		end

        neighborhood = extract(all_events, 4);
        lexical = extract(all_events, 2);
        position = extract(all_events,3);
        lvn_events = cell(size(all_events));
        pos_events = cell(size(all_events));
        
        for idx = 1:length(lvn_events)
            switch lexical{idx}
                case '1'
                    lvn_events{idx}=neighborhood{idx};
                case '2'
                    lvn_events{idx}=[neighborhood{idx} '0'];
                case '3'
                    lvn_events{idx}=[neighborhood{idx} '00'];
            end
        end
        for idx = 1:length(lvn_events)
            switch position{idx}
                case '0'
                    pos_events{idx}=neighborhood{idx};
                case '1'
                    pos_events{idx}=[neighborhood{idx} '001'];
                case '2'
                    pos_events{idx}=[neighborhood{idx} '002'];
                case '3'
                    pos_events{idx}=[neighborhood{idx} '003'];
            end
        end
        
        lvn_codes = unique(lvn.condition_index);
        lvn_events_num = str2double(lvn_events);
        for idx=1:length(lvn_codes)
            lvn_orig_idx(lvn.condition_index==lvn_codes(idx))=find(lvn_events_num==lvn_codes(idx));
        end
        
        pos_codes=unique(pos.condition_index);
        pos_events_num=str2double(pos_events);
        for idx=1:length(pos_codes)
            pos_orig_idx(pos.condition_index==pos_codes(idx))=find(pos_events_num==pos_codes(idx));
        end
        
%        position_mat = cell2mat(position);
%        half_codes=unique(half.condition_index);
%        for idx=1:length(half_codes)
%            half_pos_counts(idx).code = half_codes(idx);
%            half_pos_counts(idx).first = sum(position_mat(lvn_orig_idx(half.condition_index==half_codes(idx)))=='1');
%            half_pos_counts(idx).second = sum(position_mat(lvn_orig_idx(half.condition_index==half_codes(idx)))=='2');
%            half_pos_counts(idx).third = sum(position_mat(lvn_orig_idx(half.condition_index==half_codes(idx)))=='3');
%        end
%        halfcounts_file = sprintf('%s/%s/epochs/%s_%s_half_counts', svm_dir, subject, subject, roi);
%        save(halfcounts_file, 'half_pos_counts');
%        
%        lexical_mat = cell2mat(lexical);
%        third_codes=unique(third.condition_index);
%        for idx=1:length(third_codes)
%            third_lex_counts(idx).code = third_codes(idx);
%            third_lex_counts(idx).words = sum(lexical_mat(pos_orig_idx(third.condition_index==third_codes(idx)))=='2');
%            third_lex_counts(idx).nonwords = sum(lexical_mat(pos_orig_idx(third.condition_index==third_codes(idx)))=='3');
%        end
%        thirdcounts_file = sprintf('%s/%s/epochs/%s_%s_third_counts', svm_dir, subject, subject, roi);
%        save(thirdcounts_file, 'third_lex_counts');
        
    end
end
